﻿// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2023 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

namespace Abc.Web.Mvc.Areas.HtAdmin.Controllers;

[NonValidation]
//[AuthorizePolicy]
[AllowAnonymous]
[MaMvc(area: "HtAdmin", controllerDescription: "菜单", PCode = "HtAdmin_Developer")]
public class MenuController : HtAdminBaseController
{
    private readonly ILogger<MenuController> _logger;
    private readonly IHtMenuService _menuService;

    public MenuController(ILogger<MenuController> logger
    , IHtMenuService menuService
    )
    {
        _logger = logger;
        _menuService = menuService;
    }

    [HttpGet]
    [MaMenu(name: "菜单列表")]
    [MaPermission(name: "菜单列表", actions: "Index,GetList")]
    public async Task<IActionResult> Index()
    {
        //区域
        ViewBag.Areas = await _menuService.GetMenuAreas();

        return View();
    }

    [UnifyResult(typeof(HtMenuOut))]
    [HttpGet, HttpPost]
    public async Task<JsonResult> GetList(HtMenuQuery searchquery)
    {
        searchquery.Limit = 9999;
        var pagedlist = await _menuService.GetAllByPageToLayui(searchquery);
        return Json(pagedlist);
    }

    [MaPermission(name: "菜单详情", actions: "Info")]
    public async Task<ActionResult> Info(int? id)
    {
        HtMenuOut entity = null;
        if (id.HasValue)
        {
            entity = await _menuService.Get(id.Value);
        }
        return View(entity ?? new HtMenuOut());
    }

    [HttpPost]
    [MaPermission(name: "菜单编辑", actions: "Edit")]
    public async Task<JsonResult> Edit(HtMenuInput input)
    {
        await _menuService.InsertOrUpdate(input);
        return Json("");
    }

    [HttpPost]
    [MaPermission(name: "删除", actions: "Delete")]
    public async Task<ActionResult> Delete(string ids)
    {
        await _menuService.BatchFakeDelete(ids);
        return Json("");
    }

    [UnifyResult(typeof(Dtree))]
    [MaPermission(name: "获取菜单树", actions: "GetMenuTrees")]
    public async Task<JsonResult> GetMenuTrees()
    {
        return Json(await _menuService.GetDtree());
    }

    [HttpPost]
    [MaPermission(name: "设置状态", actions: "SetState")]
    public async Task<JsonResult> SetState(int id, bool state, string type)
    {
        await _menuService.SetState(id, state, type);
        return Json("");
    }

    ///// <summary>
    ///// 选择菜单权限
    ///// </summary>
    ///// <param name="menuid"></param>
    ///// <returns></returns>
    //[MaPermission(name: "选择菜单权限", actions: "SelectPermission,GetPermissionDtree")]
    //public async Task<IActionResult> SelectPermission(int menuid = 0)
    //{
    //    var menu = await _menuService.Get(menuid);
    //    return View(menu);
    //}

    ///// <summary>
    ///// 获取权限数据
    ///// </summary>
    ///// <returns></returns>
    //[UnifyResult(typeof(Dtree))]
    //public async Task<JsonResult> GetPermissionDtree(int menuid, string typecode = "HtAdmin")
    //{
    //    var permissionDtree = await _htSysService.GetDtreeCheck(menuid, typecode);
    //    return Json(permissionDtree);
    //}

    ///// <summary>
    ///// 设置菜单权限
    ///// </summary>
    ///// <param name="menuid"></param>
    ///// <param name="permissionstrs"></param>
    ///// <returns></returns>
    //[MaPermission(name: "设置菜单权限", actions: "SetMenuPermission")]
    //public async Task<JsonResult> SetMenuPermission(int id, string permissionstrs)
    //{
    //    return Json(await _menuService.SetMenuPermission(id, permissionstrs));
    //}
}